﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

using System.Xml;
using System.Xml.Serialization;

namespace Optimization.Experiment
{
    public class ExperimentCollection: List<ExperimentDescriptor>
    {
        static public ExperimentCollection Create(string Xml)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(Xml);
            MemoryStream ms = new MemoryStream(buffer);
            return Create(ms);
        }

        static public XmlSerializer CreateXmlSerializer(Type typeSerializer)
        {
            /*
            // Create the XmlAttributeOverrides object.
            XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
            XmlAttributes attrs = new XmlAttributes();

            // Create an XmlElementAttribute to override the 
            //field that returns d objects. The overridden field
            //returns double objects instead. 
            XmlElementAttribute attr = new XmlElementAttribute();
            attr.ElementName = "d";
            attr.Type = typeof(double);

            // Add the element to the collection of elements.
            attrs.XmlElements.Add(attr);

            attrOverrides.Add(typeof(Solution), "Solution", attrs);
            attrOverrides.Add(typeof(ParticleXml), "ParticleXml", attrs);

            XmlSerializer result = new XmlSerializer(typeSerializer, attrOverrides);
            */
            XmlSerializer result = new XmlSerializer(typeSerializer);
            return result;
        }

        static public ExperimentCollection Create(Stream stream)
        {
            XmlSerializer serializer = CreateXmlSerializer(typeof(ExperimentCollection));// new XmlSerializer(typeof(ExperimentCollection));

            return (ExperimentCollection)serializer.Deserialize(stream);
        }

        public ExperimentCollection()
        {
        }

        public ExperimentCollection(ExperimentDescriptor[] edArray): this()
        {
            foreach (ExperimentDescriptor ed in edArray)
                Add(ed.Copy());
        }

        public void Save(string XmlFileName)
        {
            FileStream fs = new FileStream(XmlFileName, FileMode.Create);

            try
            {
                Save(fs);
                //fs.Flush();
            }
            finally
            {
                //fs.Close();
            }
        }

        public void Save(Stream stream)
        {
            XmlSerializer serializer = CreateXmlSerializer(typeof(ExperimentCollection));//new XmlSerializer(typeof(ExperimentCollection));

            TextWriter writer = new StreamWriter(stream);
            serializer.Serialize(writer, this);
            writer.Close();
        }

        public string Save()
        {
            MemoryStream ms = new MemoryStream();
            Save(ms);

            return Encoding.UTF8.GetString(ms.GetBuffer());
        }
    }
}
